﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;

namespace PreContractWeb
{
    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {

        public TrustAllCertificatePolicy()

        { }

        public bool CheckValidationResult(ServicePoint sp, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest req, int problem)
        {

            return true;

        }

    }



    public class LotusDataRequester
    {
        public static bool GetDataByAuthentication(string strURL, out string buf)
        {
            buf = "";

            System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); 

            try
            {


                //Uri url=new Uri(strURL,false);
                HttpWebRequest request;
                request = (HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST"; //Post请求方式

                string username = BEGDC.Utility.Security.EncryptTool.Decrypt(System.Configuration.ConfigurationManager.AppSettings["Lotus_User"],BEGDC.Utility.Configuration.ConfigConst.Key);
                string pwd = BEGDC.Utility.Security.EncryptTool.Decrypt(System.Configuration.ConfigurationManager.AppSettings["Lotus_Password"],BEGDC.Utility.Configuration.ConfigConst.Key);

                NetworkCredential myCred = new NetworkCredential(username, pwd, string.Empty);
                request.Credentials = myCred;

                //Joan 2008-10-17 don't use proxy
                request.Proxy = null;
                //end
                request.Timeout = 600000;

                request.ContentType = "text/html;charset=utf-8"; //内容类型
                string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
                byte[] payload;
                payload = System.Text.Encoding.GetEncoding("utf-8").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节 
                request.ContentLength = payload.Length; //设置请求的ContentLength
                Stream writer = request.GetRequestStream(); //获得请求流
                writer.Write(payload, 0, payload.Length); //将请求参数写入流
                writer.Close(); //关闭请求流
                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse(); //获得响应流
                Stream s;
                s = response.GetResponseStream();
                StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("utf-8"));
                string HTML = "";
                string sLine = "";
                int i = 0;
                while (sLine != null)
                {
                    i++;
                    sLine = objReader.ReadLine();
                    if (sLine != null)
                        HTML += sLine;
                }
                //HTML = HTML.Replace("&lt;","<");
                //HTML = HTML.Replace("&gt;",">");
                buf = HTML;
                return true;
            }
            catch (Exception x)
            {
                buf = x.Message.ToString();
                return false;
            }
        }


        public static bool GetData(string strURL, out string buf)
        {
            buf = "";
            try
            {
 

                //Uri url=new Uri(strURL,false);
                HttpWebRequest request;
                request = (HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST"; //Post请求方式

                //Joan 2008-10-17 don't use proxy
                request.Proxy = null;
                //end

                request.ContentType = "text/html;charset=utf-8"; //内容类型
                string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
                byte[] payload;
                payload = System.Text.Encoding.GetEncoding("utf-8").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节 
                request.ContentLength = payload.Length; //设置请求的ContentLength
                Stream writer = request.GetRequestStream(); //获得请求流
                writer.Write(payload, 0, payload.Length); //将请求参数写入流
                writer.Close(); //关闭请求流
                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse(); //获得响应流
                Stream s;
                s = response.GetResponseStream();
                StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("utf-8"));
                string HTML = "";
                string sLine = "";
                int i = 0;
                while (sLine != null)
                {
                    i++;
                    sLine = objReader.ReadLine();
                    if (sLine != null)
                        HTML += sLine;
                }
                //HTML = HTML.Replace("&lt;","<");
                //HTML = HTML.Replace("&gt;",">");
                buf = HTML;
                return true;
            }
            catch (Exception x)
            {
                buf = x.Message.ToString();
                return false;
            }
        }

    }
}
